package com.wyd.wenyongdaAdmin.admin.dict.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.wyd.wenyongdaAdmin.commons.model.admin.dict.dto.DictEntryDTO;
import com.wyd.wenyongdaAdmin.commons.util.ResultEnum;
import com.wyd.wenyongdaAdmin.commons.util.ResultUtil;
import com.wyd.wenyongdaAdmin.xo.admin.dict.service.IDictEntryService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "字典控制器")
@RestController
@RequestMapping("/dict/entry")
public class DictEntryController {

    private static final Logger log = LoggerFactory.getLogger(DictEntryController.class);

    private final IDictEntryService dictEntryService;

    @Autowired
    DictEntryController (IDictEntryService dictEntryService) {
        this.dictEntryService = dictEntryService;
    }

    @PostMapping("/get/page")
    public ResultUtil selectDictEntryPageWithCount(@RequestBody Map<String, Object> reqParams) {
        DictEntryDTO dictEntryDTO = null;
        if (ObjectUtil.isNotNull(reqParams.get("dictId"))) {
            dictEntryDTO = new DictEntryDTO();
            dictEntryDTO.setDictId(Long.valueOf(reqParams.get("dictId").toString()));
        }
        Integer pageNumber = (Integer) reqParams.get("pageNumber");
        Integer pageSize = (Integer) reqParams.get("pageSize");
        PageInfo<DictEntryDTO> pageInfo = this.dictEntryService.selectPageWithCount(pageNumber, pageSize, dictEntryDTO);
        return ResultUtil.ok().data("records", pageInfo);
    }

    @PostMapping("/save")
    public ResultUtil addDictEntry(@RequestBody Map<String, Object> reqParams) {
        DictEntryDTO dictEntryDTO = BeanUtil.fillBeanWithMap(reqParams, new DictEntryDTO(), false);
        if (this.dictEntryService.addDictEntry(dictEntryDTO) == 0) {
            return ResultUtil.error(ResultEnum.DICT_ENTRY_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @PutMapping("/update")
    public ResultUtil updateDictEntry(@RequestBody Map<String, Object> reqParams) {
        DictEntryDTO dictEntryDTO = BeanUtil.fillBeanWithMap(reqParams, new DictEntryDTO(), false);
        if (this.dictEntryService.updateDictEntry(dictEntryDTO) == 0) {
            return ResultUtil.error(ResultEnum.DICT_ENTRY_ERROR_OR_UNKNOWN);
        } else {
            return ResultUtil.ok();
        }
    }

    @DeleteMapping("/delete")
    public ResultUtil deleteDictEntry(@RequestBody Map<String, Object> reqParams) {
        if (ObjectUtil.isNotNull(reqParams.get("ids"))) {
            List<Long> ids = Convert.toList(Long.TYPE ,reqParams.get("ids"));
            if (this.dictEntryService.deleteDictEntry(ids) == 0) {
                return ResultUtil.error(ResultEnum.DICT_ENTRY_ERROR_OR_UNKNOWN);
            } else {
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultEnum.UNKNOWN_ERROR);
    }


}
